<?php

namespace common\models;

use app\models\AdminLog;
use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "view".
 *
 * @property string $id
 * @property string $name
 * @property string $alias
 * @property string $type
 * @property string $remark
 */
class View extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'view';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'alias'], 'required'],
            ['type', 'in', 'range' => ['栏目', '内容']],
            [['name', 'alias'], 'string', 'max' => 64],
            [['name'], 'unique'],
            [['alias'], 'unique'],
            ['remark', 'string', 'max' => 1000]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'alias' => '视图值',
            'type' => '类型',
            'remark' => '备注',
        ];
    }

    public function attributeHints()
    {
        return ['alias' => '确保模板管理<b style="color: red">目录页</b>下有该文件名'];
    }

    public function afterDelete()
    {
        parent::afterDelete();
        AdminLog::log("删除视图：" . $this->alias, "delete");
    }

    /**
     * 获取目录
     * @return array
     */
    public static function getDir()
    {
        $list = View::find()->where("type='栏目'")->asArray()->all();
        return ArrayHelper::map($list, 'alias', 'name');
    }

    /**
     * 获取内容
     * @return array
     */
    public static function getPage()
    {
        $list = View::find()->where("type='内容'")->asArray()->all();
        return ArrayHelper::map($list, 'alias', 'name');
    }
}
